package eppic.pkg;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.content.Context;

public class Expense {
	
	public int id;
	public int parent_id;
	public int is_project;
	public String name;
	public float cost;
	
	public Expense()
	{
		this.id = 0;
		this.parent_id = 0;
		this.is_project = 0;
		this.name = "";
		this.cost = 0;
		
	}
	
	public Expense(int id, int parent_id, int is_project, String name, float cost)
	{
		this.id = id;
		this.parent_id = parent_id;
		this.is_project = is_project;
		this.name = name;
		this.cost = cost;
		
	}
	
	public Expense(int parent_id, int is_project, String name, float cost)
	{
		this.parent_id = parent_id;
		this.is_project = is_project;
		this.name = name;
		this.cost = cost;
		
	}
	
	public Expense(int id, Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
        Cursor c=db.rawQuery("SELECT * FROM expenses WHERE _id="+id, null);
        c.moveToFirst();
       
    	this.id = c.getInt(c.getColumnIndex("_id"));
    	this.parent_id = c.getInt(c.getColumnIndex("parent_id"));
    	this.is_project = c.getInt(c.getColumnIndex("is_project"));
    	this.name = c.getString(c.getColumnIndex("name"));
    	this.cost = c.getFloat(c.getColumnIndex("cost"));


        c.close();
        db.close();
	}
	
	
	public boolean delete(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        try{
        	db.execSQL("DELETE FROM expenses WHERE _id="+this.id);
        }catch(Exception e)
        {
        	return false;
        }
        
		return true;
	}
	
	
	public boolean save(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("parent_id", this.parent_id);
		cv.put("is_project", this.is_project);
		cv.put("name", this.name);
		cv.put("cost", this.cost);
		
		if(db.insert("expenses", "parent_id", cv) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	
	}
	
}
	
	

